/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormUpdatePotongan.java
 *
 * Created on Nov 6, 2012, 1:33:56 PM
 */
package updatepotongan;


import Karyawan.Karyawan;
import UpdateKaryawan.UpdateKaryawan;
import hitunggaji.Gaji;
import hitunggaji.HitungGajiForm;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import sigajikaryawan.ProsesUpdate;
import updateGaji.UpdateGaji;
import updateGaji.updategajikontrol;

/**
 *
 * @author admin
 */
public class FormUpdatePotongan extends javax.swing.JFrame {

    /**
     * Creates new form FormUpdatePotongan
     */
    public FormUpdatePotongan() {
        try {
            initComponents();
            jTextFieldnip.requestFocus();
            jButton1.setEnabled(false);
            List<potongan> kategori = updatepotongankontrol.getKoneksi().getAllgolongan();
            updatepotongantabelmodel kategoriTableModel = new updatepotongantabelmodel(kategori);
            jTablepotongan.setModel(kategoriTableModel);
        } catch (SQLException ex) {
            Logger.getLogger(UpdateKaryawan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldpinjaman = new javax.swing.JTextField();
        jTextFieldnip = new javax.swing.JTextField();
        ubahjButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTablepotongan = new javax.swing.JTable();
        exitjButton = new javax.swing.JButton();

        jButton1.setText("jButton1");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(51, 51, 51));

        jLabel1.setBackground(new java.awt.Color(0, 0, 0));
        jLabel1.setFont(new java.awt.Font("Papyrus", 3, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("Update Potongan");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(194, 194, 194)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(34, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(153, 153, 153));

        jLabel2.setText("NIP");

        jLabel3.setText("Pinjaman");

        jTextFieldnip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldnipActionPerformed(evt);
            }
        });

        ubahjButton.setText("Ubah");
        ubahjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubahjButtonActionPerformed(evt);
            }
        });

        jTablepotongan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "NIP", "Pinjaman"
            }
        ));
        jTablepotongan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTablepotonganMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(jTablepotongan);

        exitjButton.setText("Exit");
        exitjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(91, 91, 91)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldnip, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                            .addComponent(jTextFieldpinjaman))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(229, 229, 229)
                .addComponent(ubahjButton)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(exitjButton)
                .addGap(46, 46, 46))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldnip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldpinjaman, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(33, 33, 33)
                .addComponent(ubahjButton)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(exitjButton)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldnipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldnipActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jTextFieldnipActionPerformed

    private void exitjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitjButtonActionPerformed
        new ProsesUpdate().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_exitjButtonActionPerformed

    private void ubahjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ubahjButtonActionPerformed
//        String nip = jTextFieldnip.getText();
//        String pinjaman = jTextFieldpinjaman.getText();
//        double pin = Double.parseDouble(pinjaman);
//        
//        potongan kategoriBaru = new potongan();
//        Karyawan k = new Karyawan();
//        k.setNIP(nip);
//        kategoriBaru.setPinjaman(pin);
//        
//        try {
//            updatepotongankontrol kategoriKontrol = updatepotongankontrol.getKoneksi();
//            kategoriKontrol.updateDBPrepared(kategoriBaru);
//            
//            table();
//            JOptionPane.showMessageDialog(this, "Data berhasil diupdate");
//            jTextFieldnip.setText(nip);
//            jTextFieldpinjaman.setText(pinjaman);
//
//        } catch (SQLException ex) {
//            JOptionPane.showMessageDialog(this, "Data tidak diupdate");
//            Logger.getLogger(UpdateGaji.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        table();
         String golongan = jTextFieldnip.getText();
        String gajigolongan = jTextFieldpinjaman.getText();
        double gagol = Double.parseDouble(gajigolongan);
        potongan kategoriBaru = new potongan();
        Karyawan k = new Karyawan();
        k.setNIP(golongan);
        kategoriBaru.setNIP(k);
        
        kategoriBaru.setPinjaman(gagol);

        try {
           
                updatepotongankontrol kategoriKontrol = updatepotongankontrol.getKoneksi();
            kategoriKontrol.updateDBPrepared(kategoriBaru);
            table();
            JOptionPane.showMessageDialog(this, "Data berhasil diupdate");
            jTextFieldnip.setText(golongan);
            jTextFieldpinjaman.setText(gajigolongan);

            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data tidak diupdate");
            Logger.getLogger(UpdateGaji.class.getName()).log(Level.SEVERE, null, ex);
        }
        table();
    }//GEN-LAST:event_ubahjButtonActionPerformed

    private void jTablepotonganMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTablepotonganMouseClicked
        int row = jTablepotongan.getSelectedRow();
        String nip = jTablepotongan.getValueAt(row, 0).toString();
        String pinjaman = jTablepotongan.getValueAt(row, 1).toString();
        jTextFieldnip.setText(nip);
        jTextFieldpinjaman.setText(pinjaman);

        jButton1.setEnabled(true);
    }//GEN-LAST:event_jTablepotonganMouseClicked

    public void table() {
        String pinjaman = jTextFieldpinjaman.getText();
        try {
            List<potongan> karyawan = updatepotongankontrol.getKoneksi().getAllgolongan();
            updatepotongantabelmodel kategoriTableModel = new updatepotongantabelmodel(karyawan);
            jTablepotongan.setModel(kategoriTableModel);
        } catch (SQLException ex) {
            Logger.getLogger(UpdateKaryawan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormUpdatePotongan().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton exitjButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTablepotongan;
    private javax.swing.JTextField jTextFieldnip;
    private javax.swing.JTextField jTextFieldpinjaman;
    private javax.swing.JButton ubahjButton;
    // End of variables declaration//GEN-END:variables
}
